package eco.lamp.bl;

import android.content.res.Resources;
import eco.lamp.R;

public class LampUtil {

	final static int[][] INC_EFF = new int[][] { new int[] { 1, 5 },
			new int[] { 5, 25 }, new int[] { 15, 110 }, new int[] { 25, 200 },
			new int[] { 35, 350 }, new int[] { 40, 500 },
			new int[] { 50, 700 }, new int[] { 55, 800 },
			new int[] { 60, 850 }, new int[] { 65, 1000 },
			new int[] { 70, 1100 }, new int[] { 75, 1200 },
			new int[] { 90, 1450 }, new int[] { 95, 1600 },
			new int[] { 100, 1700 }, new int[] { 135, 2350 },
			new int[] { 150, 2850 }, new int[] { 200, 3900 },
			new int[] { 300, 6200 } };

	public static int getOutputByPower(int power) {
		int result = 0, prev = 0;
		for (int i = 0; i < INC_EFF.length; i++) {
			if (INC_EFF[i][0] == power) {
				result = INC_EFF[i][1];
				break;
			} else {
				if (prev < i) {
					if (INC_EFF[i][0] < power) {
						prev = i;
					} else if (INC_EFF[i][0] > power) {
						int dPowerEnergy = ((INC_EFF[i][1] - INC_EFF[prev][1]) / (INC_EFF[i][0] - INC_EFF[prev][0]));
						result = INC_EFF[prev][1] + (power - INC_EFF[prev][0])
								* dPowerEnergy;
						break;
					}
				}
			}
		}
		return result;
	}

	public static int getOutput(int lampType, int power) {
		int result = 1;
		switch (lampType) {
		case LAMP_TYPE_FLUORESCENT:
			result = 5;
			break;
		case LAMP_TYPE_LED:
			result = 8;
			break;
		default:
			break;
		}

		result = getOutputByPower(power * result);
		return result;
	}

	public static int getLife(int lampType) {
		int result = 1;
		switch (lampType) {
		case LAMP_TYPE_FLUORESCENT:
			result = 8;
			break;
		case LAMP_TYPE_LED:
			result = 50;
			break;
		default:
			break;
		}
		result *= 1000;
		return result;
	}

	public static int getMaxPower(int lampType) {
		int result = 300;
		switch (lampType) {
		case LAMP_TYPE_FLUORESCENT:
			result = 60;
			break;
		case LAMP_TYPE_LED:
			result = 30;
			break;
		default:
			break;
		}
		return result;
	}

	public static int getDefaultPower(int lampType) {
		int result = 75;
		switch (lampType) {
		case LAMP_TYPE_FLUORESCENT:
			result = 15;
			break;
		case LAMP_TYPE_LED:
			result = 10;
			break;
		default:
			break;
		}
		return result;
	}

	public final static int LAMP_TYPE_INCADESCENT = 1;
	public final static int LAMP_TYPE_FLUORESCENT = 2;
	public final static int LAMP_TYPE_LED = 3;

	public static String getLampTypeName(int lampType, Resources r) {
		switch (lampType) {
		case LAMP_TYPE_INCADESCENT:
			return r.getString(R.string.strInc);
		case LAMP_TYPE_FLUORESCENT:
			return r.getString(R.string.strFlr);
		case LAMP_TYPE_LED:
			return r.getString(R.string.strLed);
		default:
			return "";
		}
	}
}
